package com.project.biz.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsHomeConfBiz;
import com.project.biz.CmsProductBiz;
import com.project.biz.CmsProductRecommendBiz;
import com.project.biz.CmsRecoveryOrderBiz;
import com.project.entity.CmsHomeConf;
import com.project.mapper.CmsHomeConfMapper;
import com.project.pojo.dto.HomeConfTypeDTO;
import com.project.pojo.dto.HomeDTO;
import com.project.pojo.dto.HomeRecoveryOrderDTO;
import com.project.pojo.dto.ProductDTO;
import com.project.pojo.model.AddHomeConfModel;
import com.project.pojo.model.GetRecommendProductModel;
import com.project.pojo.model.ProductModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 移动端首页配置业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/11/20 15:29
 * @Version: 1.0
 */
@Service
public class CmsHomeConfBizImpl extends ServiceImpl<CmsHomeConfMapper, CmsHomeConf> implements CmsHomeConfBiz {

    /**
     * 商品业务层接口类
     */
    @Autowired
    private CmsProductBiz cmsProductBiz;

    /**
     * 回收订单业务层接口类
     */
    @Autowired
    private CmsRecoveryOrderBiz recoveryOrderBiz;

    /**
     * 商品推荐业务层接口类
     */
    @Autowired
    private CmsProductRecommendBiz productRecommendBiz;

    /**
     * 修改移动端首页配置信息
     * @param addHomeConfModel
     * @return
     */
    public int updateHomeConf(AddHomeConfModel addHomeConfModel){
        QueryWrapper<CmsHomeConf> queryWrapper = new QueryWrapper<>();
        CmsHomeConf homeConf = this.baseMapper.selectOne(queryWrapper);
        if (null == homeConf){
            homeConf = new CmsHomeConf();
        }
        homeConf.setRotationChart(JSONObject.toJSONString(addHomeConfModel.getRotationChart()));
        homeConf.setType1(JSONObject.toJSONString(addHomeConfModel.getType1()));
        homeConf.setType2(JSONObject.toJSONString(addHomeConfModel.getType2()));
        homeConf.setType3(JSONObject.toJSONString(addHomeConfModel.getType3()));
        homeConf.setType4(JSONObject.toJSONString(addHomeConfModel.getType4()));
        homeConf.setCenterAdvert(JSONObject.toJSONString(addHomeConfModel.getCenterAdvert()));
        homeConf.setCenterProduct(JSONObject.toJSONString(addHomeConfModel.getCenterProduct()));
        homeConf.setRecommendProduct("已废弃,首页底部推荐商品保存在推荐表");
        if (null == homeConf.getHomeId()){
            return this.baseMapper.insert(homeConf);
        }else {
            return this.baseMapper.updateById(homeConf);
        }
    }

    /**
     * 获取移动端首页配置
     * @return
     */
    public HomeDTO getHomeConf(){
        HomeDTO homeDTO = new HomeDTO();

        QueryWrapper<CmsHomeConf> queryWrapper = new QueryWrapper<>();
        CmsHomeConf homeConf = this.baseMapper.selectOne(queryWrapper);
        if (null != homeConf){
            homeDTO.setRotationChart(JSONObject.parseArray(homeConf.getRotationChart(), String.class));
            if (StringUtils.isNotBlank(homeConf.getType1())){
                homeDTO.setType1(JSONObject.parseObject(homeConf.getType1(), HomeConfTypeDTO.class));
            }
            if (StringUtils.isNotBlank(homeConf.getType2())){
                homeDTO.setType2(JSONObject.parseObject(homeConf.getType2(), HomeConfTypeDTO.class));
            }
            if (StringUtils.isNotBlank(homeConf.getType3())){
                homeDTO.setType3(JSONObject.parseObject(homeConf.getType3(), HomeConfTypeDTO.class));
            }
            if (StringUtils.isNotBlank(homeConf.getType4())){
                homeDTO.setType4(JSONObject.parseObject(homeConf.getType4(), HomeConfTypeDTO.class));
            }

            //首页中部广告
            if (StringUtils.isNotBlank(homeConf.getRotationChart())){
                homeDTO.setCenterAdvert(JSONObject.parseArray(homeConf.getRotationChart(), String.class));
            }

            //首页中部商品
            if (StringUtils.isNotBlank(homeConf.getCenterProduct())){
                List<Long> centerProductIds = JSONObject.parseArray(homeConf.getCenterProduct(), Long.class);
                ProductModel productModel = new ProductModel();
                productModel.setOrderById(2);
                productModel.setState(1);//状态（0:下架,1:上架）
                productModel.setProductIds(centerProductIds);
                List<ProductDTO> centerProducts = this.cmsProductBiz.selectProductList(productModel);
                homeDTO.setCenterProduct(centerProducts);
            }

            //首页底部推荐商品,从推荐表获取
            GetRecommendProductModel getRecommendProductModel = new GetRecommendProductModel();
            getRecommendProductModel.setType(5);//推荐类型（1:秒杀,2:满减,3:每日推荐,4:美味推荐,5:首页底部）
            List<ProductDTO> recommendProduct = this.productRecommendBiz.selectRecommendProductList(getRecommendProductModel);
            homeDTO.setRecommendProduct(recommendProduct);
        }

        //获取最新三条回收订单
        List<HomeRecoveryOrderDTO> recoveryOrderDTOS = this.recoveryOrderBiz.getNewRecoveryOrders(3);
        homeDTO.setRecoveryOrders(recoveryOrderDTOS);

        return homeDTO;
    }

}
